package com.lzh.intercepter;

import com.lzh.pojo.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class LoginIntercepter implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //逻辑：判断用户是否进行登录 本质：判断session中是否存在user
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        System.out.println("---------------------------------->"+user);
        if (user == null){
            //没有登录
            response.sendRedirect(request.getContextPath()+"/user/login");
            return false;
        }
        //放行找目标资源
        return true;
    }
}
